// JavaScript Document
var Spritesheet = Class.extend({
	name: "",
	sprites: {},
	init: function(name) {
		this.name = name;
	},
	setSprite: function(sprite_num, ax, ay, awidth, aheight) {
		this.sprites[sprite_num] = 
		{
			x: ax,
			y: ay,
			width: awidth, 
			height: aheight
		};
	},
	draw: function(sprite_num, x, y) {
		try {  
    		ctx.drawImage(images[this.name], 
    			this.sprites[sprite_num].x, this.sprites[sprite_num].y,
    			this.sprites[sprite_num].width, this.sprites[sprite_num].height,
    			x, y, 
    			this.sprites[sprite_num].width, this.sprites[sprite_num].height);
        } catch(e) {alert(e)}
	},
	a: function() {
		alert("KKK");
	}
});


var GameObject = Class.extend({
	x: 0,
	y: 0,
	init: function(x, y) {
		this.x = x;
		this.y = y;
	},
	setPos: function(x, y) {
		this.x = x;
		this.y = y;
	},
	draw: function() {
		try {  
            ctx.drawImage(images["TilesSprite2"], 0, 0, grid, grid, 
            	this.x, this.y, grid, grid);
        } catch(e) {alert(e)}
	},
	a: function() {
		alert("KKK");
	}
});

function lenghtDir(ax, ay, radius, dir)
{
	return {
				x: ax + radius * Math.cos(dir),
				y: ay + radius * Math.sin(dir)
				
		   }
}

function mouseDir(posx, posy) {
	return dir = Math.atan2((mouse.x - posx), -(mouse.y - posy));
}

function rotDir() {
	/*
	rotspeed = 0.1 * speed / 32;

	if (Math.Abs(rotation - newRotation) < rotspeed * time || 
		Math.Abs(rotation - newRotation) > MathHelper.TwoPi - rotspeed * time)
	{
		rotation = newRotation;
	}
	else
	{
		if ((rotation < newRotation || rotation - newRotation > Math.PI) && !(newRotation - rotation > Math.PI))
		{
		rotation += rotspeed * time;
		}
		else
		{
		rotation -= rotspeed * time;
		}
	}*/
}

function drawPart(spritesheet,sprite,absDir, partDir, radius, orignX, orignY, flip, posDir, expandRotation, x, y) {
	ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.translate(camera.x + x, camera.y + y);
    var signflip = -1;
    if (flip) {
    	signflip = 1;
    }
    ctx.scale(camera.scale * signflip, camera.scale);
    var pos = lenghtDir(0,0,radius,-posDir + absDir * signflip);
    ctx.translate(pos.x, pos.y);
    ctx.rotate(absDir * signflip + partDir * signflip + expandRotation);
    spriteSheets[spritesheet].draw(sprite, -orignX, -orignY);
}

var Player = GameObject.extend({
	x_move: 0,
	y_move: 0,
	speed: 10,
	dir: 0,
	eyedir: 0,
	feet1dir: -0.4,
	feet2dir: 0.4,
	pickaxedir: 0.3,
	walkcycle: 0,
	time: 0,
	init: function() {
	},
	setPos: function(x, y) {
		this._super(x, y);
	},
	moveX: function(n) {
		this.x_move += n;
	},
	moveY: function(n) {
		this.y_move += n;
	},
	isMoving: function() {
		return (this.x_move != 0 || this.y_move != 0);
	},
	update: function() {
		//this.dir += 0.1;
		//this.eyedir += 0.03;
		this.walkcycle += this.speed / 30;
		this.time++;
		this.pickaxedir = 0.3 + 0.3 * Math.sin(this.time/10);
		this.updatePosition();
		this.eyedir = mouseDir(camera.x, camera.y); 
		/*
		if (this.eyedir > this.dir + 1) {
			this.eyedir = this.dir + 1
		} else if (this.eyedir < this.dri + 1) {
			this.eyedir = this.dri + 1;
		}*/
	},
	draw: function () {
		ctx.save();
		ctx.rotate(this.dir);
		//spriteSheets["minion"].draw("feet",0,0);
		drawPart("minion","feet",this.dir, this.feet1dir, 
			15, 17 + 10 * -Math.sin(this.walkcycle), 30, true,
			3 + Math.sin(this.walkcycle), 0.6 + 0.2 * Math.sin(this.walkcycle), this.x, this.y);
		drawPart("minion","feet",this.dir, this.feet2dir, 
			15, 17 + 10 * Math.sin(this.walkcycle), 30, false,
			3 + Math.sin(-this.walkcycle), 0.6 - 0.2 * Math.sin(this.walkcycle), this.x, this.y);
			
		drawPart("minion","pickaxe",this.dir, this.pickaxedir, 30, 13, 43, false, 0.2, 0.6, this.x, this.y);
		drawPart("minion","body",this.dir, 0, 0, 33, 45, false, 0, 0, this.x, this.y);
		var eyetype = "eye";
		if ((this.time % 100) < 4) {
			eyetype = "closedeye";
		}
    	drawPart("minion",eyetype,this.dir, this.eyedir - this.dir, 16, 10, 10, true, 2.4, 0.6, this.x, this.y);
		drawPart("minion",eyetype,this.dir, this.eyedir - this.dir, 16, 10, 10, false, 2.4, 0.6, this.x, this.y);
    	//spriteSheets["minion"].draw("pickaxe",0,0);
    	ctx.restore();
	},
	updatePosition: function() {
		if (this.x_move > 0) {
			this.x_move -= this.speed;
			this.x += this.speed;
			if (this.x_move < 0) {
				this.x += this.x_move;
				this.x_move = 0;
			}
		}
		if (this.x_move < 0) {
			this.x_move += this.speed;
			this.x -= this.speed;
			if (this.x_move > 0) {
				this.x += this.x_move;
				this.x_move = 0;
			}
		}
		if (this.y_move > 0) {
			this.y_move -= this.speed;
			this.y += this.speed;
			if (this.y_move < 0) {
				this.y += this.y_move;
				this.y_move = 0;
			}
		}
		if (this.y_move < 0) {
			this.y_move += this.speed;
			this.y -= this.speed;
			if (this.y_move > 0) {
				this.y += this.y_move;
				this.y_move = 0;
			}
		}
	}
});